package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.UserDao;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.event.CorrectRespEvent;
import com.hangzhou.xyj.model.type.ECorrectType;
import com.hangzhou.xyj.net.handler.UpdateNameHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.UpdateNameReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.widget.CommonTitleBarView.OnNavRightListener;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.ToastUtil;

public class MyCorrectNameActivity extends BaseActivity {
    EditText mEtName;
    Intent intent;

    public static final int SUCCESS = 1;
    public static final int FAIL = 0;

    @Override
    public void initTop() {
        enableTitle(true, "名字");
        enableRightNav(true, "确定");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_correct_name);
    }

    @Override
    public void initViews() {
        mEtName = (EditText) findViewById(R.id.et_name);

    }

    @Override
    public void initDatas() {
        intent = getIntent();
        mEtName.setText(intent.getStringExtra("value"));
        mEtName.setSelection(mEtName.length());
    }

    @Override
    public void initListeners() {
        setOnNavRightListener(new OnNavRightListener() {
            @Override
            public void onNavRight() {
                final Intent intent = getIntent();// 获取的是启动该Activity之前的Activity对应的Intent
                final String name = mEtName.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtil.show("名字不能为空！");
                    return;
                } else if (!PublicUtil.checkStrLength(name, 6)) {
                    ToastUtil.show("名字长度最多6个字，请检查");
                    return;
                }
                //将更新的数据发送到服务器
                UpdateNameReq updateNameReq = new UpdateNameReq(Integer.valueOf(CommonPreferences.getUserId(mContext)), name);
                HttpLoader.getInstance().sendRequest(new UpdateNameHandler(updateNameReq, new MyHttpResponseListener() {
                    @Override
                    public void onSuccess(Object model) {
                        super.onSuccess(model);
                        //更新应用中的信息
                        XYJApplication.getUserInfo().setNickName(name);
                        //更新本地数据库中的信息
                        UserDao dao = new UserDao(mContext);
                        dao.addUser(XYJApplication.getUserInfo());
                        //提示成功
                        ToastUtil.show("保存成功！");
                        intent.putExtra("value", name);
                        CorrectRespEvent event = new CorrectRespEvent();
                        event.setTag(ECorrectType.NAME.getValue());
                        event.setValue(name);
                        EventBusUtil.postEvent(event);
                        MyCorrectNameActivity.this.setResult(SUCCESS, intent);// 1表示成功
                        MyCorrectNameActivity.this.finish();// 关闭本窗口
                    }

                    @Override
                    public void onFail(String msg) {
                        super.onFail(msg);
                        ToastUtil.show("修改失败，请重试！");
                        MyCorrectNameActivity.this.setResult(FAIL, intent);// 1表示成功
                    }
                }, 0));
            }
        });
    }
}
